// pages/personal.js
import {
  record
} from '../../api/personal/index'
let startY = 0;
let moveY = 0;
let moveDistance = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTranslate: "0",
    coverTransition: '',
    userInfo: {},
    recordList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 从本地存储中获取用户信息
    let info = wx.getStorageSync("userInfo");
    console.log(info);
    this.setData({
      userInfo: JSON.parse(info),
    })
    this.getRecordList();
  },
  getRecordList: async function () {
    let recordResp = await record({
      uid: this.data.userInfo.userId,
      type: 1
    });
    console.log(recordResp);
    let recordList = recordResp.weekData.slice(0, 10).map((item, index) => {
      item.id = index;
      return item;
    });
    this.setData({
      recordList,
    })
  },

  handleTouchStart: function (event) {
    console.log("touchstart");
    this.setData({
      coverTransition: ''
    });
    console.log(event);
    startY = event.touches[0].clientY;
  },

  handleTouchMove: function (event) {
    console.log("touchmove");
    this.setData({
      coverTransition: ''
    });
    moveY = event.touches[0].clientY;
    moveDistance = moveY - startY;
    if (moveDistance <= 0) {
      return;
    }
    if (moveDistance > 200) {
      moveDistance = 200;
    }
    this.setData({
      coverTranslate: moveDistance + 'rpx',
      coverTransition: "transform .5s linear"
    });
  },

  handleTouchEnd: function (event) {
    console.log("touchend");
    this.setData({
      coverTranslate: "0"
    });
  },
  toLogin: function () {
    // if (this.data.userInfo.userId) {
    //   return;
    // }
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})